package com.niubility.controller;


import com.niubility.entity.College;
import com.niubility.exception.CustomException;
import com.niubility.server.IColService;
import com.niubility.vo.TrueFalse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
*@Author : GongGeng
*@FinishDate : 2018-01-27
*@Description : the class of college controller layer
*/



@Controller
public class CollegeController {
    @Autowired
    private IColService collegeService;

    Logger logger = Logger.getLogger("CollegeController");


    //显示热门学校，根据学校有的社团数量排序
    @RequestMapping("/displayHotCol")
    public void  displayHotCol(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        String callback=req.getParameter("callback");
        int colCounts=0;
        String result = null;
        if(req.getParameter("colCounts")!=null)
            colCounts= Integer.parseInt(req.getParameter("colCounts"));
        try {
            List<College> collegeList = collegeService.loadHotCol(colCounts);
             result = callback + "(" + JSONArray.fromObject(collegeList).toString() + ")";
        }catch (Exception e){
            result=callback+"("+"{\"returncade\":"+"108}"+")";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //根据学校名字获取学校数据
    @RequestMapping("/loadColByName")
    public void loadColByName(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        String callback=req.getParameter("callback");
        String colName=req.getParameter("colName");
        String result=null;
        try {
            College college = collegeService.loadByColName(colName);
            result = callback + "(" + JSONObject.fromObject(college).toString() + ")";
        }catch (Exception e){
            result=callback+"("+"{\"returncade\":"+"108}"+")";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    //根据学校ID获取学校数据
    @RequestMapping("/loadColById")
    public void loadColById(HttpServletRequest req,HttpServletResponse resp) throws CustomException {
        String callback=req.getParameter("callback");
        int colId=Integer.parseInt(req.getParameter("colId"));
        String result=null;

        try {
            College college = collegeService.loadByColId(colId);
             result = callback + "(" + JSONObject.fromObject(college).toString() + ")";
        }catch (Exception e){
            result = callback+"("+"{\"returncade\":"+"108}"+")";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
